package com.hengpeng.msls.controller.borrow;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.itfinbase.en.BorrowStatus;
import com.hengpeng.itfinbase.persist.Account;
import com.hengpeng.itfinbase.persist.BorrowInfo;
import com.hengpeng.itfinbase.persist.RewardRedpacket;
import com.hengpeng.itfinbase.persist.UserInfo;
import com.hengpeng.msls.controller.BaseController;
import com.hengpeng.msls.vo.SessionMember;
import com.hengpeng.msls.vo.invest.InvestInfoVo;

@Controller
@RequestMapping("/borrowInfo")
public class BorrowInfoController extends BaseController {
    
    @RequestMapping(value="list")
    public String borrowList(HttpServletRequest request,ModelMap map){
        int size=100000;
        BorrowStatus[] status = { BorrowStatus.RAISE, BorrowStatus.END, BorrowStatus.LOANING,
                BorrowStatus.REPAYMENTING, BorrowStatus.REPAYMENTED };
        ServiceResult<List<BorrowInfo>> serviceResult = serviceFactory.getBorrowInfoService().queryList(status, null,
                1, size);
        map.put("productList", serviceResult.getReturnValue());
        return "/page/borrow/list.jsp";
    }
    
    /**
     * 投资标详情
     * @param request
     * @param map
     * @param borrowId
     * @return
     */
    @RequestMapping(value="detail")
    public String borrowDetail(HttpServletRequest request,ModelMap map,String borrowId){
        BorrowInfo borrowInfo = serviceFactory.getBorrowInfoService().getBorrowInfo(Long.parseLong(borrowId), null);
        /*List<InvestInfo> resultInvestInfos = serviceFactory.getInvestInfoService().queryInvestList(
                Long.parseLong(borrowId), null, null);*/
        List<InvestInfoVo> resultInvestInfos = serviceFactory.getInvestInfoService().queryInvestListHistory(
                Long.parseLong(borrowId), null, null);
        map.put("borrowInfo", borrowInfo);
        map.put("investList", resultInvestInfos);
        
        return "/page/borrow/detail.jsp";
    }
    
    /**
     * 投资页
     */
    @RequestMapping(value="invest")
    public String borrowInvest(HttpServletRequest request,ModelMap map,String borrowId,String redId){
        SessionMember session = getSessionMember(request);
        if (session==null) {
            return "/user/login/index.jsp";
        }
        UserInfo user = session.getUser();
        Account account = serviceFactory.getAccountService().getAccount(user.getId());
        if (StringUtils.isNotBlank(redId)) {
            RewardRedpacket redPacket = serviceFactory.getPersistService().get(RewardRedpacket.class, Long.valueOf(redId));
            map.put("redPacket", redPacket);
        }
        BorrowInfo borrowInfo = serviceFactory.getBorrowInfoService().getBorrowInfo(Long.parseLong(borrowId), null);
        map.put("account", account);
        map.put("borrowInfo", borrowInfo);
        return "/page/borrow/invest.jsp";
    }
}
